📋 Contents
By the end of this class session, students will be able to:
- Understand the purpose of control statements in loops.
- Use
breakto exit a loop immediately. - Use
continueto skip the current iteration and move to the next. - Use
passas a placeholder in an otherwise-empty block. - Differentiate clearly between
break,continueandpass. - Apply the correct control statement in search, filtering and menu-driven programs.
- Recognize the effect of
breakinside nested loops and the loopelseclause.
By default, a loop runs from start to finish, executing every iteration. But sometimes we need to alter this normal flow — stop early, skip a step, or leave a placeholder. Python provides three control statements for this purpose:
| Statement | Purpose | Effect on Loop |
|---|---|---|
break | Exit the loop immediately | Loop stops |
continue | Skip the rest of the current iteration | Loop continues with next value |
pass | Do nothing (placeholder) | Loop continues normally |
Why do we need control statements?
- Searching: Stop as soon as the target is found — no need to keep looping.
- Filtering: Skip values that don't meet a condition.
- Early termination: Exit a loop when an error or invalid input is detected.
- Placeholders: Mark code that will be filled in later, without causing an error.
for and while loops.break Statement3.1 Definition
The break statement immediately terminates the loop in which it appears. Control jumps to the first statement after the loop.
3.2 Syntax
for variable in sequence:
if condition:
break
statement(s)
3.3 Key Points
- Exits the innermost loop only (in a nested loop).
- Works with both
forandwhile. - Any statements after
breakin the same block are skipped. - If the loop has an
elseclause, theelseis skipped whenbreakexecutes.
Flowchart — break
Fig 1. Flowchart showing break — exit the loop as soon as the target is found.
3.4 Example Programs — break
Example 1: Break out of a for loop
for i in range(1, 11):
if i == 5:
break
print(i)
print("Loop ended.")
Explanation: When i reaches 5, break exits the loop. Values 5 to 10 are not printed.
Example 2: Search for an element in a list
numbers = [10, 20, 30, 40, 50]
target = int(input("Enter number to search: "))
for num in numbers:
if num == target:
print(target, "found in the list!")
break
else:
print(target, "not found.")
Example 3: break in a while loop
n = 1
while True:
print(n)
n += 1
if n > 5:
break
Example 4: Menu-driven program using break
while True:
print("\n--- Menu ---")
print("1. Say Hello")
print("2. Say Bye")
print("3. Exit")
choice = input("Enter choice: ")
if choice == "1":
print("Hello!")
elif choice == "2":
print("Bye!")
elif choice == "3":
print("Exiting...")
break
else:
print("Invalid choice.")
Example 5: break in a nested loop (exits only inner loop)
for i in range(1, 4):
for j in range(1, 4):
if j == 2:
break
print("i =", i, " j =", j)
Explanation: The break exits the inner loop only. The outer loop continues normally.
continue Statement4.1 Definition
The continue statement skips the rest of the current iteration and jumps to the next iteration of the loop. The loop does not terminate.
4.2 Syntax
for variable in sequence:
if condition:
continue
statement(s)
4.3 Key Points
- Statements after
continuein the same block are skipped for the current iteration. - Works with both
forandwhile. - In a
whileloop, the condition must eventually becomeFalse, otherwise you get an infinite loop. - The loop
elseclause does execute if the loop finishes normally (nobreak).
Flowchart — continue
Fig 2. Flowchart showing continue — skip the rest of the current iteration and move to the next.
4.4 Example Programs — continue
Example 6: Print only odd numbers (skip even)
for i in range(1, 11):
if i % 2 == 0:
continue
print(i)
Example 7: Sum only positive numbers in a list
numbers = [10, -5, 20, -8, 30, -2, 40]
total = 0
for num in numbers:
if num < 0:
continue
total += num
print("Sum of positive numbers:", total)
Example 8: continue in a while loop
n = 0
while n < 10:
n += 1
if n == 5:
continue
print(n)
Example 9: Skip vowels in a string
word = "programming"
for ch in word:
if ch in "aeiou":
continue
print(ch, end=" ")
Example 10: Print only multiples of 3 in a range
for i in range(1, 21):
if i % 3 != 0:
continue
print(i, end=" ")
pass Statement5.1 Definition
The pass statement is a null statement — it does nothing at runtime. It is used as a placeholder where Python syntax requires a statement but no action is needed.
5.2 Syntax
if condition:
pass # to be implemented later
5.3 Key Points
- Does nothing — it is a placeholder.
- Prevents a syntax error in an empty block.
- Useful during development when you plan to fill in code later.
- Can be used inside
for,while,if, functions and classes.
Flowchart — pass
Fig 3. Flowchart showing pass — a no-operation placeholder that lets the loop continue normally.
5.4 Example Programs — pass
Example 11: pass as a placeholder in a loop
for i in range(1, 6):
if i == 3:
pass # TODO: implement special case for 3
else:
print(i)
Example 12: pass in an empty function
def my_function():
pass # to be implemented later
my_function()
print("Function called successfully.")
Example 13: pass in an empty class
class Student:
pass # class body to be defined later
s = Student()
print("Object created:", s)
Example 14: pass inside if block
x = 10
if x > 0:
pass # positive number — nothing to do
else:
print("Non-positive")
print("Program finished.")
break vs continue vs pass| Feature | break | continue | pass |
|---|---|---|---|
| Effect | Exit the loop entirely | Skip rest of current iteration | Do nothing |
| Loop continues? | ❌ No | ✅ Yes | ✅ Yes |
| Skips remaining statements? | Yes (rest of loop) | Yes (current iteration) | No |
Loop else executes? | No | Yes | Yes |
| Common use | Search, early exit | Filtering, skipping | Placeholder, empty block |
| Typical code | if num == target: break | if num < 0: continue | def f(): pass |
break and continue change the flow of the loop; pass is only a syntactic placeholder and has no effect at runtime.Example 15: break and continue together
for i in range(1, 11):
if i == 8:
break # stop the loop at 8
if i % 2 == 0:
continue # skip even numbers
print(i) # prints odd numbers before 8
Explanation: Even numbers are skipped (continue); when i reaches 8, break ends the loop.
Example 16: break with loop else
numbers = [10, 20, 30, 40, 50]
target = int(input("Enter target: "))
for num in numbers:
if num == target:
print("Found", target)
break
else:
print(target, "not found.")
Explanation: If break executes, the else block is skipped. If the loop finishes without break, else runs.
Example 17: Password attempt with continue
correct_password = "python123"
for attempt in range(1, 4):
password = input(f"Attempt {attempt} — Enter password: ")
if password != correct_password:
print("Wrong password. Try again.")
continue
print("Login successful!")
break
else:
print("Account locked. Too many failed attempts.")
Example 18: Skip multiples of 3 and stop at 15
for i in range(1, 21):
if i == 15:
break
if i % 3 == 0:
continue
print(i, end=" ")
Example 19: pass placeholder with continue
for i in range(1, 8):
if i == 4:
pass # skip — no action for now
continue
if i == 6:
break
print(i)
Explanation: At i = 4, pass does nothing and continue skips the print. At i = 6, break exits the loop.
| Mistake | Correction |
|---|---|
Expecting break to exit both loops in a nested loop | break exits only the innermost loop |
Using continue when you meant break | Ask: do you want to exit or skip? |
Placing continue after the loop variable update in a while | Place the update before the continue, otherwise infinite loop |
Using pass expecting it to skip a line | pass does nothing — use continue to skip |
Empty block without pass | Use pass to satisfy the syntax |
Assuming loop else runs after a break | Loop else is skipped when break executes |
Forgetting the colon after if/while/for | Always end with : |
while + continue: In a while loop, always update the loop variable before the continue statement. Otherwise, the condition never changes and you get an infinite loop.🔍 Activity 1 — Predict the Output
What will the following code print? Work it out on paper first, then verify.
for i in range(1, 8):
if i == 3:
continue
if i == 6:
break
print(i, end=" ")
🐛 Activity 2 — Debug the Code
What is wrong with the code below? Fix it.
n = 0
while n < 5:
if n == 2:
continue
print(n)
n += 1
Hint: What happens to n when it reaches 2?
✏️ Activity 3 — Write the Program
Write a Python program using a loop that:
- Asks the user to enter numbers repeatedly.
- Stops when the user enters 0.
- Skips negative numbers using
continue. - Prints the sum of positive numbers entered.
🧩 Activity 4 — Menu with Exit
Write a menu-driven program using while True and break with options:
- 1. Print "Hello"
- 2. Print "World"
- 3. Exit
🧪 Activity 5 — Search with for-else
Given names = ["Alice", "Bob", "Charlie", "David"], write a program that asks the user for a name and prints whether it is found. Use the loop else clause with break.
- What is the purpose of control statements in loops?
- Differentiate between
breakandcontinuewith examples. - What is the difference between
continueandpass? - When does the
elseclause of a loop execute? - How does
breakbehave inside a nested loop? - Why can
continuecause an infinite loop in awhileloop? How can you avoid it? - Write a Python program to find the first even number in a list using
break. - Write a Python program to print only the odd numbers from 1 to 20 using
continue. - Write a Python program that uses
passas a placeholder for a function that is not yet implemented. - Explain the output of:
for i in range(1, 6): if i == 3: continue if i == 5: break print(i)
breakExits the loop immediately. Use for search and early termination. Exits only the innermost loop in nested loops.continueSkips the rest of the current iteration and jumps to the next. Use for filtering and skipping values.passDoes nothing — a syntactic placeholder. Use to fill empty blocks without causing errors.elseExecutes only if the loop finishes normally (no break). Useful for search failure cases.break → exit; continue → skip; pass → placeholder.while loops, update the loop variable before continue to avoid an infinite loop.Gafoor I
Assistant Professor | Department of Mathematics | NAM College Kallikkandy
Teaching Note · Module III (d) · Control Statements in Python · Academic Year 2025–26